﻿$(document).ready(function () {
    $.Jahanutility.togglePanels($("#carAdminMenuHeader"), $(".carAdminMenu"), 1);
    $.Jahanutility.togglePanels($("#houseAdminMenuHeader"), $(".houseAdminMenu"), 1);
    $.Jahanutility.togglePanels($("#goodsAdminMenuHeader"), $(".goodsAdminMenu"), 1);

    $.Jahanutility.togglePanels($("#khodro"), $(".carMenu"), 1);
    $.Jahanutility.togglePanels($("#namayeshgah"), $(".carstoreMenu"), 1);
    $.Jahanutility.togglePanels($("#khadamat"), $(".serviceMenu"), 1);
    //*************************************************************
    $.ajax({
        'type': 'POST',
        'url': '../../Services/Authorize.svc/GetCurrentUser',
        'data': '',
        'contentType': 'application/json; charset=utf-8',
        'dataType': 'json',
        'success': function (result) {
            if (!result['d'].IsSucceed) {
                hideUserMenu();
            }
            else {
                setMenu(result['d'].Data);
            }
        },
        'error': function (error) { }
    });


    //*************************************************************
    $(".fn_login").click(function () {

        $('.loginValidaion').each(function () {
            $(this).triggerHandler("focusout");
        });

        if ($('#contactform input.error').size() > 0) {
            $('#contactform ').effect('shake', { times: 3 }, 50);
            return false;
        }

        var userName = $.trim($(".userName").val());
        var pass = $.trim($(".pass").val());

        $.ajax({
            'type': 'POST',
            'url': '../../Services/Authorize.svc/LoginCheck',
            'data': JSON.stringify({ userName: userName, atttemptPass: pass }),
            'contentType': 'application/json; charset=utf-8',
            'dataType': 'json',
            'success': function (result) {
                if (!result['d'].IsSucceed) {
                    $.kish.message.showErrorMessage(result['d'].Message.Message);
                }
                else {
                    setMenu(result['d'].Data);
                }
            },
            'error': function (error) { }
        });

    });


    //*************************************************************
    $(".fn_logOut").click(function () {

        $.ajax({
            url: '../../Services/Authorize.svc/LogOut',
            data: '',
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (result['d'] != null) {
                    if (!result['d'].IsSucceed) {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                    else {
                        //     window.location.replace("~/Account/Login.aspx")
                        hideUserMenu();
                    }
                }
            }
        });
    });


    //*************************************************************
    function setMenu(user) {
        if (user.Roles == 1) {
            $('.adminMenu_fn').slideDown();
            $('#AdminName').html(user.FirstName + ' ' + user.LastName);
            $('.LoginPanel_fn').hide();
        }
        //means user is member
        else if (user.Roles == 2) {
            $('.userMenu_fn').slideDown();
            $('#userName').html(user.FirstName + ' ' + user.LastName);
            $('.LoginPanel_fn').hide();
        }

    };

    //*************************************************************
    function hideUserMenu() {
        //means user is not a member
        $('.userMenu_fn').hide();
        $('.adminMenu_fn').hide();
        $('.pass').val('رمز عبور');
        $('.LoginPanel_fn').slideDown();

    };


    //*************************************************************
    if ($.browser.mozilla) {
        $(".LoginPanel_fn").keypress(checkForEnter);
    } else {
        $(".LoginPanel_fn").keydown(checkForEnter);
    }
    function checkForEnter(event) {
        if (event.keyCode == 13) {
            $(".fn_login").click();
        }
    }

});
